package com.gm.wholesale.module.report.ui.tab;

import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.github.gzuliyujiang.calendarpicker.OnRangeDatePickListener;
import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.util.timeUtils.DateTimeUtils;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.timePickerView.TimePickerViewUtils;
import com.gm.wholesale.R;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.report.bean.CustormReportBean;
import com.gm.wholesale.module.report.bean.GoodReportBean;
import com.gm.wholesale.module.report.model.ReportModel;
import com.gm.wholesale.module.report.view.ReportTabTimeView;
import com.gm.wholesale.module.report.view.ShowCustomReportView;

import java.util.Date;
import java.util.List;

public class GoodReportFragment extends BaseFragment implements View.OnClickListener {
    private EditText goodNameEd;
    private ReportTabTimeView todayTv;
    private ReportTabTimeView weekTv;
    private ReportTabTimeView monthTv;
    private TextView choiceTimeTv;
    private ListView listView;
    private String mTimeBegin;
    private String mTimeEnd;

    private String mChoiceTime;

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_good_report, parent);
        initView(view);
    }

    private void initView(View view) {
        goodNameEd = view.findViewById(R.id.goodNameEd);
        todayTv = view.findViewById(R.id.todayTv);
        weekTv = view.findViewById(R.id.weekTv);
        monthTv = view.findViewById(R.id.monthTv);
        choiceTimeTv = view.findViewById(R.id.choiceTimeTv);
        listView = view.findViewById(R.id.listView);
        todayTv.showCheck();
        mTimeBegin = DateTimeUtils.formatDateTime(System.currentTimeMillis(), DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
        mChoiceTime=mTimeBegin+" 至 "+mTimeBegin;
        mTimeEnd=mTimeBegin;
        setChoiceTime();
        getData();
        todayTv.setOnClickListener(this);
        weekTv.setOnClickListener(this);
        monthTv.setOnClickListener(this);
        choiceTimeTv.setOnClickListener(this);
        goodNameEd.setOnEditorActionListener((v, actionId, event) -> {

            if (actionId== EditorInfo.IME_ACTION_SEARCH) {
                //todo
                getData();
            }
            return false;


        });
    }
    private void setChoiceTime(){
        choiceTimeTv.setText(mChoiceTime);
    }

    private void getData(){
        ArrayMap<String, Object> queryData = new ArrayMap<>();
        queryData.put("dateBegin", mTimeBegin+" 00:00:00");
        queryData.put("dateEnd", mTimeEnd+" 23:59:59");
        queryData.put("skuName",goodNameEd.getText().toString());

        ReportModel.getGoodReport(null, queryData, new BaseHttpObserver<BasePageData<GoodReportBean>>() {
            @Override
            protected void handleResultOkResp(BasePageData<GoodReportBean> resp) {
                showView(resp.getData().getDatas());
            }
        });
    }
    private void showView(List<GoodReportBean> reportGoodBeans){
        listView.setAdapter(new CommonAdapter<GoodReportBean>(getContext(),reportGoodBeans,R.layout.list_good_report_item) {
            @Override
            public void convert(ViewHolder viewHolder, GoodReportBean data) {
                viewHolder.setText(R.id.goodNameTv,data.getSkuName());
                viewHolder.setText(R.id.saleNumberTv,data.getSalesQuantity());
                viewHolder.setText(R.id.salePriceTv,data.getSalesAmount()+"元");
                viewHolder.getView(R.id.showDetailTv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showCustormByGoods(data);
                    }
                });
            }
        });
    }

    private void showCustormByGoods(GoodReportBean goodReportBean){
        GoodBean goodBean=new GoodBean();
        goodBean.setName(goodReportBean.getSkuName());
        goodBean.setId(goodReportBean.getSkuID());
        ShowCustomReportView showCustomReportView = new ShowCustomReportView(getActivity());
        showCustomReportView.showGoodReport(goodBean, (IBaseView) getActivity());
        Popupwindow.showPopCenterView(getActivity(), showCustomReportView);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.todayTv) {
            mTimeBegin = DateTimeUtils.formatDateTime(System.currentTimeMillis(), DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
            mChoiceTime=mTimeBegin+" 至 "+mTimeBegin;
            setChoiceTime();
            todayTv.showCheck();
            weekTv.showUnCheck();
            monthTv.showUnCheck();
            getData();
        } else if (view.getId() == R.id.weekTv) {
            mTimeBegin = DateTimeUtils.getWeekStart();
            mTimeEnd = DateTimeUtils.getWeekEnd();
            mChoiceTime=mTimeBegin+" 至 "+mTimeEnd;
            todayTv.showUnCheck();
            weekTv.showCheck();
            monthTv.showUnCheck();
            setChoiceTime();
            getData();
        } else if (view.getId() == R.id.monthTv) {
            mTimeBegin = DateTimeUtils.getMonthStart();
            mTimeEnd = DateTimeUtils.getMonthEnd();
            mChoiceTime=mTimeBegin+" 至 "+mTimeEnd;
            todayTv.showUnCheck();
            weekTv.showUnCheck();
            monthTv.showCheck();
            setChoiceTime();
            getData();
        }
        else if (view.getId()==R.id.choiceTimeTv){
            long startTime=DateTimeUtils.getTimeStringToLong(mTimeBegin+" 00:00:01",DateTimeUtils.LONG_DATETIME_FORMAT_STRING);
            long endTime=DateTimeUtils.getTimeStringToLong(mTimeEnd+" 23:59:59",DateTimeUtils.LONG_DATETIME_FORMAT_STRING);
            CalendarPicker calendarPicker= TimePickerViewUtils.showTimePicker(getActivity(),startTime,endTime);
            calendarPicker.setOnRangeDatePickListener(new OnRangeDatePickListener() {
                @Override
                public void onRangeDatePicked(@NonNull Date startDate, @NonNull Date endDate) {
                    setViewUnCheck(todayTv);
                    setViewUnCheck(weekTv);
                    setViewUnCheck(monthTv);
                    mTimeBegin=DateTimeUtils.formatDateTime(startDate,DateTimeUtils.YMD);
                    mTimeEnd=DateTimeUtils.formatDateTime(endDate,DateTimeUtils.YMD);
                    mChoiceTime=mTimeBegin +" 至 "+mTimeEnd;
                    setChoiceTime();
                    getData();
                }
            });
        }

    }

    private void setViewUnCheck(ReportTabTimeView view){
        view.showUnCheck();
    }
}
